/* File:        	PoliciesManagement.java
* Package:			org.esi.authz.pdp.policy
* Created:      	29-abr-2011
* Last Changed: 	0:08:09
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.pdp.policy;

import java.net.URI;

import javax.xml.bind.JAXBElement;

import org.jboss.security.xacml.core.model.policy.ActionMatchType; 
import org.jboss.security.xacml.core.model.policy.ActionType; 
import org.jboss.security.xacml.core.model.policy.ActionsType; 
import org.jboss.security.xacml.core.model.policy.ApplyType; 
import org.jboss.security.xacml.core.model.policy.AttributeValueType; 
import org.jboss.security.xacml.core.model.policy.ConditionType;
import org.jboss.security.xacml.core.model.policy.EffectType;
import org.jboss.security.xacml.core.model.policy.ExpressionType;
import org.jboss.security.xacml.core.model.policy.FunctionType;
import org.jboss.security.xacml.core.model.policy.ObjectFactory; 
import org.jboss.security.xacml.core.model.policy.PolicyType;
import org.jboss.security.xacml.core.model.policy.ResourceMatchType;
import org.jboss.security.xacml.core.model.policy.ResourceType;
import org.jboss.security.xacml.core.model.policy.ResourcesType;
import org.jboss.security.xacml.core.model.policy.RuleType;
import org.jboss.security.xacml.core.model.policy.SubjectAttributeDesignatorType;
import org.jboss.security.xacml.core.model.policy.TargetType;
import org.jboss.security.xacml.factories.PolicyAttributeFactory;
import org.jboss.security.xacml.interfaces.XACMLConstants;
import org.jboss.security.xacml.interfaces.XMLSchemaConstants;

// TODO: Auto-generated Javadoc
/**
 * The Class PoliciesManagement.
 */
public class PoliciesManagement {

	/**
	 * Instantiates a new policies management.
	 */
	public PoliciesManagement() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * Construct policy.
	 *
	 * @return the policy type
	 * @throws Exception the exception
	 */
	public static PolicyType ConstructPolicy() throws Exception
   {
		ObjectFactory objectFactory = new ObjectFactory();
		PolicyType policyType = new PolicyType();
		policyType.setPolicyId("ExamplePolicy");
		policyType.setVersion("2.0");
		policyType.setRuleCombiningAlgId(XACMLConstants.RULE_COMBINING_PERMIT_OVERRIDES);
		
		//Create a target
		TargetType targetType = new TargetType();
		ResourcesType resourcesType = new ResourcesType();
		ResourceType resourceType = new ResourceType();
		ResourceMatchType rmt = new ResourceMatchType();
		rmt.setMatchId(XACMLConstants.FUNCTION_ANYURI_EQUAL);
		rmt.setResourceAttributeDesignator(PolicyAttributeFactory.createAttributeDesignatorType(
				XACMLConstants.ATTRIBUTEID_RESOURCE_ID, XMLSchemaConstants.DATATYPE_ANYURI, null, false));
		rmt.setAttributeValue(PolicyAttributeFactory.createAnyURIAttributeType(new URI("http://test/developer-guide.html")));
		resourceType.getResourceMatch().add(rmt);
		resourcesType.getResource().add(resourceType);

		targetType.setResources(resourcesType);

		policyType.setTarget(targetType);

		//Create a Rule
		RuleType permitRule = new RuleType();
		permitRule.setRuleId("ReadRule");
		permitRule.setEffect(EffectType.PERMIT);
		
		ActionsType permitRuleActionsType = new ActionsType();
		ActionType permitRuleActionType = new ActionType();
		
		ActionMatchType amct = new ActionMatchType();
		amct.setMatchId("urn:oasis:names:tc:xacml:1.0:function:string-equal");
		amct.setAttributeValue(PolicyAttributeFactory.createStringAttributeType("read"));
		amct.setActionAttributeDesignator(PolicyAttributeFactory.createAttributeDesignatorType(
        XACMLConstants.ATTRIBUTEID_ACTION_ID, XMLSchemaConstants.DATATYPE_STRING, null, false));
		permitRuleActionType.getActionMatch().add(amct);
		TargetType permitRuleTargetType = new TargetType();
		permitRuleActionsType.getAction().add(permitRuleActionType);
		permitRuleTargetType.setActions(permitRuleActionsType);
		permitRule.setTarget(permitRuleTargetType);

		ConditionType permitRuleConditionType = new ConditionType();
		FunctionType functionType = new FunctionType();
		functionType.setFunctionId(XACMLConstants.FUNCTION_STRING_EQUAL);
		JAXBElement<ExpressionType> jaxbElementFunctionType = objectFactory.createExpression(functionType);
		permitRuleConditionType.setExpression(jaxbElementFunctionType);

		ApplyType permitRuleApplyType = new ApplyType();
		permitRuleApplyType.setFunctionId(XACMLConstants.FUNCTION_STRING_IS_IN);

		SubjectAttributeDesignatorType sadt = PolicyAttributeFactory.createSubjectAttributeDesignatorType(
        XACMLConstants.ATTRIBUTEID_ROLE, XMLSchemaConstants.DATATYPE_STRING, null, false, null);
		JAXBElement<SubjectAttributeDesignatorType> sadtElement = objectFactory.createSubjectAttributeDesignator(sadt);
		AttributeValueType avt = PolicyAttributeFactory.createStringAttributeType("developer");
		JAXBElement<AttributeValueType> jaxbAVT = objectFactory.createAttributeValue(avt);
		permitRuleApplyType.getExpression().add(jaxbAVT);
		permitRuleApplyType.getExpression().add(sadtElement);

		permitRuleConditionType.setExpression(objectFactory.createApply(permitRuleApplyType));

  	  	permitRule.setCondition(permitRuleConditionType);

  	  	policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(permitRule);
  	  	//Create a Deny Rule
  	  	RuleType denyRule = new RuleType();
  	  	denyRule.setRuleId("DenyRule");
  	  	denyRule.setEffect(EffectType.DENY);
  	  	policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(denyRule);

  	  	return policyType;
   }
}
